﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using LearnNet.Common;
namespace Advertise
{
    public class ReportHelper
    {

        public static bool SetAttribute(string xmlNodePath, string attributeValue, System.Windows.Forms.WebBrowser webBrowser1, XmlHelper xmlHelper)
        {
            XmlNode node = xmlHelper.GetNode(xmlNodePath);

            if (node != null)
            {
                if (node.Attributes["enable"] == null || node.Attributes["enable"].Value == "1")
                {
                    if (node.Attributes["key"] == null)
                    {
                        return false;
                    }
                    if (node.Attributes["name"] != null)
                    {
                        HtmlElementCollection htmlCollection = webBrowser1.Document.GetElementsByTagName(node.Attributes["name"].Value);
                        for (int i = 0; i < htmlCollection.Count; i++)
                        {
                            HtmlElement element = htmlCollection[i];
                            string searchAttribute = string.Empty;
                            if (node.Attributes["searchAttribute"] == null)
                                searchAttribute = "name";
                            else
                                searchAttribute = node.Attributes["searchAttribute"].Value;

                            if (element.GetAttribute(searchAttribute) == node.Attributes["key"].Value)
                            {
                               // element.Focus();
                                if (node.Attributes["setAttribute"] == null)
                                {
                                    element.SetAttribute("value", attributeValue);
                                }
                                else
                                {
                                    element.SetAttribute(node.Attributes["setAttribute"].Value, attributeValue);
                                }
                            }

                        }

                    }
                    else
                    {


                        HtmlElement element = webBrowser1.Document.GetElementById(node.Attributes["key"].Value);
                        if (element == null)
                        {
                            return false;
                        }
                        //element.Focus();
                        if (node.Attributes["setAttribute"] == null)
                        {
                            element.SetAttribute("value", attributeValue);
                        }
                        else
                        {
                            element.SetAttribute(node.Attributes["setAttribute"].Value, attributeValue);
                        }
                    }

                    return true;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  模拟点击链接:
        /// </summary>
        /// <param name="url"></param>

        public  static void HerfClick(System.Windows.Forms.WebBrowser webBrowser1, string url)
        {
            for (int i = 0; i < webBrowser1.Document.All.Count; i++)
            {
                if (webBrowser1.Document.All[i].TagName.ToUpper() == "A" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim() == url)
                {
                    webBrowser1.Document.All[i].InvokeMember("click");//引发”CLICK”事件
                    
                    break;
                }
            }

        }
        /// <summary>
        /// 运行网页中的JS函数
        /// </summary>
        /// <param name="jsfun"></param>
        /// <param name="jsParameter"></param>

        public  static void CallJsMethod(System.Windows.Forms.WebBrowser webBrowser1, string jsfun, string jsParameter)//函数名字,函数参数
        {
            HtmlDocument doc = webBrowser1.Document;
            doc.InvokeScript(jsfun, new object[] { jsParameter });
        }
    }
}
